package com.atguigu.jxc.service.impl;
import com.atguigu.jxc.dao.PurchaseListDao;
import com.atguigu.jxc.dao.PurchaseListGoodsDao;
import com.atguigu.jxc.dao.SupplierDao;
import com.atguigu.jxc.dao.UserDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.PurchaseListService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sun.xml.internal.bind.v2.runtime.reflect.ListTransducedAccessorImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PurchaseListServiceImpl implements PurchaseListService {
    @Autowired
    private PurchaseListDao purchaseListDao;
    @Autowired
    private PurchaseListGoodsDao purchaseListGoodsDao;
    @Autowired
    private SupplierDao supplierDao;
    @Autowired
    private UserDao userDao;

    @Override
    public ServiceVO save(PurchaseList purchaseList, String purchaseListGoodsStr) {
        String purchaseNumber = purchaseList.getPurchaseNumber();
        purchaseListDao.savePurchaseList(purchaseList);
        // 使用谷歌Gson将JSON字符串数组转换成具体的集合
        Gson gson = new Gson();
        List<PurchaseListGoods> list = gson.fromJson(purchaseListGoodsStr,new TypeToken<List<PurchaseListGoods>>(){}.getType());
        for (PurchaseListGoods purchaseListGoods : list) {
            Integer purchaseListId = purchaseList.getPurchaseListId();
            PurchaseList purchaseList1 = purchaseListDao.getPurchaseListByPurchaseNumber(purchaseNumber);
            purchaseListGoods.setPurchaseListId(purchaseList1.getPurchaseListId());
            purchaseListGoodsDao.savePurchaseListGoods(purchaseListGoods);
        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public Map<String, Object> list(String purchaseNumber, Integer supplierId, Integer state, String sTime,String eTime) {
        Map<String,Object> map = new HashMap<>();
        List<PurchaseList> purchaseLists = purchaseListDao.getBillList(purchaseNumber,supplierId,state,sTime,eTime);
        List<Map<String,Object>> list = new ArrayList<>();
        for (PurchaseList purchaseList : purchaseLists) {
            Map<String,Object> internMap = new HashMap<>();
            internMap.put("purchaseListId",purchaseList.getPurchaseListId());
            internMap.put("purchaseNumber",purchaseList.getPurchaseNumber());
            internMap.put("amountPaid",purchaseList.getAmountPaid());
            internMap.put("amountPayable",purchaseList.getAmountPayable());
            internMap.put("purchaseDate",purchaseList.getPurchaseDate());
            internMap.put("remarks",purchaseList.getRemarks());
            internMap.put("state",state);
            Integer interSupplierId = purchaseList.getSupplierId();
            internMap.put("supplierId", interSupplierId);
            Supplier supplier = supplierDao.getSupplierById(interSupplierId);
            String supplierName = supplier.getSupplierName();
            internMap.put("supplierName",supplierName);
            Integer userId = purchaseList.getUserId();
            internMap.put("userId",userId);
            User user = userDao.getUserById(userId);
            internMap.put("trueName",user.getTrueName());
            list.add(internMap);
        }
        map.put("rows",list);
        return map;
    }

    @Override
    public Map<String, Object> goodsList(Integer purchaseListId) {
        Map<String,Object> map = new HashMap<>();
        List<PurchaseListGoods> purchaseListGoodsList = purchaseListGoodsDao.getGoodsList(purchaseListId);
        List<Map<String,Object>> list = new ArrayList<>();
        for (PurchaseListGoods purchaseListGoods : purchaseListGoodsList) {
            Map<String,Object> innerMap = new HashMap<>();
            innerMap.put("purchaseListGoodsId",purchaseListGoods.getPurchaseListGoodsId());
            innerMap.put("goodsId",purchaseListGoods.getGoodsId());
            innerMap.put("goodsCode",purchaseListGoods.getGoodsCode());
            innerMap.put("goodsName",purchaseListGoods.getGoodsName());
            innerMap.put("goodsModel",purchaseListGoods.getGoodsModel());
            innerMap.put("goodsUnit",purchaseListGoods.getGoodsUnit());
            innerMap.put("goodsNum",purchaseListGoods.getGoodsNum());
            innerMap.put("price",purchaseListGoods.getPrice());
            innerMap.put("total",purchaseListGoods.getTotal());
            innerMap.put("purchaseListId",purchaseListGoods.getPurchaseListId());
            innerMap.put("goodsTypeId",purchaseListGoods.getGoodsTypeId());
            list.add(innerMap);
        }
        map.put("rows",list);
        return map;
    }

    @Override
    public ServiceVO deleteByPurchaseListId(Integer purchaseListId) {
        purchaseListDao.deleteByPurchaseListId(purchaseListId);
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }
}
